package com.api.syh56.shengyunhui.config;

import com.api.syh56.shengyunhui.properties.WechatProperties;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;

import com.wechat.pay.java.service.payments.app.AppServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WechatPayConfig {

    @Autowired
    private WechatProperties wechatProperties;


    @Bean
    public RSAAutoCertificateConfig createconfig() throws Exception {
        // 初始化商户配置
        RSAAutoCertificateConfig config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(wechatProperties.getMerchantId())
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKeyFromPath(wechatProperties.getPrivateKeyPath())
                        .merchantSerialNumber(wechatProperties.getMerchantSerialNumber())
                        .apiV3Key(wechatProperties.getApiV3key())
                        .build();
        return config;
    }

    @Bean
    public JsapiServiceExtension createJsapiService() throws Exception {
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(wechatProperties.getMerchantId())
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKeyFromPath(wechatProperties.getPrivateKeyPath())
                        .merchantSerialNumber(wechatProperties.getMerchantSerialNumber())
                        .apiV3Key(wechatProperties.getApiV3key())
                        .build();
        return new JsapiServiceExtension.Builder().config(config).build();
    }

    @Bean
    public RefundService createRefundService() throws Exception {
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(wechatProperties.getMerchantId())
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKeyFromPath(wechatProperties.getPrivateKeyPath())
                        .merchantSerialNumber(wechatProperties.getMerchantSerialNumber())
                        .apiV3Key(wechatProperties.getApiV3key())
                        .build();
        return new RefundService.Builder().config(config).build();
    }

    @Bean
    public AppServiceExtension createAppServiceExtension() throws Exception {
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(wechatProperties.getMerchantId())
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKeyFromPath(wechatProperties.getPrivateKeyPath())
                        .merchantSerialNumber(wechatProperties.getMerchantSerialNumber())
                        .apiV3Key(wechatProperties.getApiV3key())
                        .build();
        return new AppServiceExtension.Builder().config(config).build();
    }

    @Bean
    public TransferBatchService createTransferBatchService(){
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(wechatProperties.getMerchantId())
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKeyFromPath(wechatProperties.getPrivateKeyPath())
                        .merchantSerialNumber(wechatProperties.getMerchantSerialNumber())
                        .apiV3Key(wechatProperties.getApiV3key())
                        .build();
        return new TransferBatchService.Builder().config(config).build();
    }


}
